package com.leyou.cart.service;

import com.leyou.cart.entity.Cart;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static String prefix="ly:cart:";

    public void addCartToRedis(String userId, Cart cart) {
//        添加购物车之前应该判断redis中是否有当前商品如果有数量累加
        BoundHashOperations<String, String, String> hashOperations = redisTemplate.boundHashOps(prefix + userId);
        addCart(cart, hashOperations);
    }



    public List<Cart> findCartListFromRedisByUserId(String userId) {
        BoundHashOperations<String, String, String> hashOperations = redisTemplate.boundHashOps(prefix + userId);
        List<String> values = hashOperations.values();  //获取所有hash中的value值
        if(CollectionUtils.isEmpty(values)){
            throw new LyException(ExceptionEnum.CARTS_NOT_FOUND);
        }
        List<Cart> cartList = values.stream().map(str -> {
            return JsonUtils.toBean(str, Cart.class);
        }).collect(Collectors.toList());
        return cartList;

    }

    public void updateCartNum(String userId, Long skuId, Integer num) {
//        修改某一个用户下的某一个购物车对象的数量
//        先把cart取出来，更改数量 还需要把更改后的cart放入到redis中
        BoundHashOperations<String, String, String> hashOperations = redisTemplate.boundHashOps(prefix + userId);
        String cartStr = hashOperations.get(skuId.toString());
        Cart cart = JsonUtils.toBean(cartStr, Cart.class);
        cart.setNum(num);
        hashOperations.put(skuId.toString(),JsonUtils.toString(cart));
    }

    public void deleteCartFromRedis(String userId, Long skuId) {
        redisTemplate.boundHashOps(prefix + userId).delete(skuId.toString());
    }

    public void addCartListToRedis(String userId, List<Cart> cartList) {
        BoundHashOperations<String, String, String> hashOperations = redisTemplate.boundHashOps(prefix + userId);
//        添加购物车
        for (Cart cart : cartList) {
            addCart(cart,hashOperations);
        }
    }

//    还是需要判断redis中是否有此商品 有 数量合并 没有没有 直接放入到redis中
    private void addCart(Cart cart, BoundHashOperations<String, String, String> hashOperations) {
        String cartStr = hashOperations.get(cart.getSkuId().toString());
//        先判断cartStr是否为空
        if(StringUtils.isNotBlank(cartStr)){
//            再把cartStr转成cart对象 数量累加
            Cart cart1 = JsonUtils.toBean(cartStr, Cart.class);
//            数量累加
//            cart1.setNum(cart1.getNum()+cart.getNum());
            cart.setNum(cart1.getNum()+cart.getNum());
        }
//            把当前cart追加到redis中
        hashOperations.put(cart.getSkuId().toString(),JsonUtils.toString(cart));
    }
}
